package com.socket;

import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.Logger;

import com.thread.HandlerThread;

public class Server {
	
	private static Logger logger = Logger.getLogger(Server.class);
	
	public static final int PORT = 12345;//监听的端口号     
    
    public static void main(String[] args) {    
        //System.out.println("服务器启动...\n");
        logger.info("服务器启动...\n");
        Server server = new Server();    
        server.init();    
    }    
    
    public void init() {    
        try {    
            ServerSocket serverSocket = new ServerSocket(PORT);
            logger.info("【服务器】port:"+serverSocket.getLocalPort());
            while (true) {
                // 一旦有堵塞, 则表示服务器与客户端获得了连接
                Socket client = serverSocket.accept();  
                logger.info("【服务器】客户端已连接!ip"+client.getInetAddress());
                // 处理这次连接
                new HandlerThread(client); 
            }    
        } catch (Exception e) {    
//            System.out.println("服务器异常: " + e.getMessage());  
        	logger.error("服务器异常: " + e.getMessage());
        }    
    }    
}
